package com.ionehe.group.common.uaa.ex.application.rest;

import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.application.converter.FunctionVOConvert;
import com.ionehe.group.common.uaa.ex.application.model.FunctionAddVO;
import com.ionehe.group.common.uaa.ex.application.model.FunctionDeleteVO;
import com.ionehe.group.common.uaa.ex.application.model.FunctionVO;
import com.ionehe.group.common.uaa.ex.application.model.FunctionUpDownVO;
import com.ionehe.group.common.uaa.ex.application.model.FunctionUpdateVO;
import com.ionehe.group.common.uaa.ex.domain.service.FunctionReadService;
import com.ionehe.group.common.uaa.ex.domain.service.FunctionWriteService;
import com.ionehe.group.common.uaa.ex.dto.FunctionDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Copyright (c) 2020 ionehe.com
 * Date: 2020/11/24
 * Time: 下午05:25
 *
 * @author 2020年 <a href="mailto:a@ionehe.com">秀</a>
 */
@RestController
@RequestMapping("/api/uaa/function")
@Api(hidden = true, value = "uaa-功能管理相关服务", tags = "uaa-功能管理相关服务")
@ApiIgnore
public class UaaFunctionController {
    @Autowired
    private FunctionReadService functionReadService;
    @Autowired
    private FunctionWriteService functionWriteService;

    /**
     * 功能列表
     *
     * @return 功能列表
     */
    @GetMapping("/list")
    @ApiOperation("功能列表")
    public Response<List<FunctionVO>> functionDetailInfo() {
        Response<List<FunctionDTO>> res = functionReadService.functionDetailInfo();
        if (!res.success()) {
            return Response.no(res.getMessage());
        }
        Response<List<FunctionVO>> yes = Response.yes(FunctionVOConvert.INSTANCE.dto2vo(res.getData()));
        yes.setMessage(null);
        return yes;
    }

    /**
     * 功能上下架
     *
     * @return 成功或失败
     */
    @PostMapping("/upDownStand")
    @ApiOperation("功能上下架")
    public Response<Boolean> upDownStand(@RequestBody FunctionUpDownVO functionUpDownVo) {
        return functionWriteService.upDownStand(functionUpDownVo.getIdList(), functionUpDownVo.getStatus());
    }

    /**
     * 功能排序
     *
     * @param idUp   排序id
     * @param idDown 排序id
     * @return 成功或失败
     */
    @PostMapping("/sort")
    @ApiOperation("功能排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idUp", value = "排序id", dataType = "Long", type = "query", required = true),
            @ApiImplicitParam(name = "idDown", value = "排序id", dataType = "Long", type = "query", required = true)
    })
    public Response<Boolean> updateSort(Long idUp, Long idDown) {
        return functionWriteService.updateSort(idUp, idDown);
    }

    /**
     * 添加同级或子级
     *
     * @param functionAddVO 添加功能vo
     * @return 成功或失败
     */
    @PostMapping("/add")
    @ApiOperation("添加同级或子级")
    public Response<Boolean> addFunction(@RequestBody FunctionAddVO functionAddVO) {
        return functionWriteService.addFunction(FunctionVOConvert.INSTANCE.vo2dto(functionAddVO));
    }

    /**
     * 更新功能
     *
     * @param functionUpdateVO 更新功能vo
     * @return 成功或失败
     */
    @PostMapping("/update")
    @ApiOperation("更新功能")
    public Response<Boolean> updateFunction(@RequestBody FunctionUpdateVO functionUpdateVO) {
        return functionWriteService.updateFunction(FunctionVOConvert.INSTANCE.vo2dto(functionUpdateVO));
    }

    /**
     * 批量删除
     *
     * @param functionDeleteVO id集合
     * @return 成功或失败
     */
    @PostMapping("/delete")
    @ApiOperation("批量删除")
    public Response<Boolean> batchDelete(@RequestBody FunctionDeleteVO functionDeleteVO) {
        return functionWriteService.batchDelete(functionDeleteVO.getIdList());
    }
}
